package com.cmwa.scbp.sys.organization.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.cmwa.scbp.base.mvc.entity.Employee;
import com.cmwa.scbp.base.mvc.entity.SearchParam;
import com.cmwa.scbp.sys.organization.dao.CompanyDao;
import com.cmwa.scbp.sys.organization.dao.OrganizationDao;
import com.cmwa.scbp.sys.organization.entity.CompanyVo;
import com.cmwa.scbp.sys.organization.entity.OrganizationVo;
import com.cmwa.scbp.sys.organization.service.OrganizationService;
import com.cmwa.scbp.utils.SessionUtils;

@Service
public class OrganizationServiceImpl implements OrganizationService {
	@Resource
	private OrganizationDao organizationDao;
	@Resource
	private CompanyDao companyDao;

	@Override
	public List<OrganizationVo> queryOrganizationListByParentId(String orgId) {
		if (StringUtils.isBlank(orgId)) {
			orgId = "0";
		}
		return organizationDao.queryOrganizationListByParentId(orgId);
	}

	@Override
	public List<OrganizationVo> queryStationListByOrgId(String orgId) {
		return organizationDao.queryStationListByOrgId(orgId);
	}

	@Override
	public OrganizationVo queryOrgByEmpId(String empId) {
		List<OrganizationVo> orgList = organizationDao.queryOrgByEmpId(empId);
		if (orgList != null && !orgList.isEmpty()) {
			return orgList.get(0);
		}
		return null;
	}

	@Override
	public List<OrganizationVo> queryOrgListByOrgName(String orgName) {
		return organizationDao.queryOrgListByOrgName(orgName);
	}

	@Override
	public List<OrganizationVo> queryOrgTreeList() {
		return organizationDao.queryOrgTreeList();
	}

	@Override
	public void insertOutOrg(OrganizationVo org) {
		OrganizationVo dbReOrg = organizationDao.getByOrgId(org.getReOrgId());
		if (dbReOrg != null) {
			org.setReOrgName(dbReOrg.getOrgName());
		}

		Employee employee = SessionUtils.getEmployee();
		org.setCreateId(employee.getID());

		organizationDao.insertOutOrg(org);

		if (StringUtils.equals(org.getReOrgId(), "0")) {
			CompanyVo c = new CompanyVo();
			c.setId(org.getOrgId());
			c.setCompanyName(org.getOrgName());
			c.setCreateId(employee.getID());
			companyDao.insertCompanyInfo(c);
		}
	}

	@Override
	public boolean checkRepeatOrgId(String orgId, String orgName) {
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("orgId", orgId);
		param.put("orgName", orgName);
		int count = organizationDao.countByCondition(param);
		return count > 0;
	}

	@Override
	public boolean checkRepeatTitleId(String orgId, String titleId) {
		int count = organizationDao.checkRepeatTitleId(orgId, titleId);
		return count > 0;
	}

	@Override
	public boolean checkRepeatOrgName(String orgId, String orgName) {
		int count = organizationDao.checkRepeatOrgName(orgId, orgName);
		return count > 0;
	}

	@Override
	public OrganizationVo getByOrgId(String orgId) {
		return organizationDao.getByOrgId(orgId);
	}

	@Override
	public List<OrganizationVo> queryOrgChilds(SearchParam sp) {
		return organizationDao.queryOrgChilds(sp);
	}

	@Override
	public List<OrganizationVo> queryTitleChilds(SearchParam sp) {
		return organizationDao.queryTitleChilds(sp);
	}

}
